//
//  UILabel+LJLabel.m
//  PianKe
//
//  Created by lanou3g on 15/7/30.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "UILabel+LJLabel.h"

@implementation UILabel (LJLabel)


- (CGFloat)heightOfLabelByStr{
    
    NSDictionary *dict = [NSDictionary dictionaryWithObject:self.font forKey:NSFontAttributeName];
    CGRect bounds = [self.text boundingRectWithSize:CGSizeMake(self.bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return bounds.size.height;
}


- (CGFloat)widthOfLabelByStr{
    
    NSDictionary *dict = [NSDictionary dictionaryWithObject:self.font forKey:NSFontAttributeName];
    CGRect bounds = [self.text boundingRectWithSize:CGSizeMake(0, self.bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return bounds.size.width;
}


- (void)adaptationHeight{

    CGRect frame = self.frame;
    frame.size.height = [self heightOfLabelByStr];
    self.numberOfLines = 0;
    self.frame = frame;
}

- (void)adaptationWidth{

    CGRect frame = self.frame;
    frame.size.width = [self widthOfLabelByStr];
    self.numberOfLines = 0;
    self.frame = frame;
}

+ (CGFloat)heightOfLabelByStr:(NSString *)str
                     fontSize:(CGFloat)fontSize
                        width:(CGFloat)width{

    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:fontSize] forKey:NSFontAttributeName];
    CGRect bounds = [str boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return bounds.size.height;
}


+ (CGFloat)heightOfLabelByStr:(NSString *)str fontSize:(CGFloat)fontSize width:(CGFloat)width lineSpacing:(CGFloat)lineSpacing{

    //富文本设置文字行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = lineSpacing;
    NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:fontSize], NSParagraphStyleAttributeName:paragraphStyle};
    NSAttributedString *attributedText =[[NSAttributedString alloc]initWithString:str attributes:attributes];
    //获取设置文本间距以后的高度
    CGRect fram = [attributedText boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    return fram.size.height;
}


+ (CGFloat)widthOfLabelByStr:(NSString *)str
                    fontSize:(CGFloat)fontSize
                      height:(CGFloat)height{

    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:fontSize] forKey:NSFontAttributeName];
    CGRect bounds = [str boundingRectWithSize:CGSizeMake(0, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return bounds.size.width;
}
@end
#pragma mark 使lable的高度根据字符串的长短改变
//返回给定字符串的高度 传进去字符串,宽度,还有字体大小
//+ (CGFloat)heightOfString:(NSString *)str
//Width:(CGFloat)width
//FontSize:(CGFloat)fontSize
//{
//    //获取在字体大小为14,显示区域宽度为250的情况下,str这个字符串的bounds
//    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:fontSize] forKey:NSFontAttributeName];
//    //这个250是introduceLable的宽度
//    CGRect bounds = [str boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
//    return bounds.size.height;
//}